/*
 * PanelArztWarteschlange.java
 *
 * Created on 19. Mai 2008, 22:19
 */

package ocumed.teams.teamb.gui;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JFrame;
import javax.swing.table.TableColumnModel;

import ocumed.teams.IOcumed;
import ocumed.teams.IPanBehandlung;
import ocumed.teams.IPatient;
import ocumed.teams.OcumedFactory;
import ocumed.teams.teamb.application.PatientReihung;

/**
 *
 * @author  apr3455
 */
public class PArztWarteschlange extends javax.swing.JPanel {
      
    PatientReihung m_controller = new PatientReihung();
    WarteschlangeArzt m_tableModel = null;
    Integer m_arztId;
    IPanBehandlung m_iPanBehandlung = null;
    
    /** Creates new form PanelWarteschlange */
    public PArztWarteschlange(Integer arztId, IPanBehandlung iPanBehandlung) {
        m_arztId = arztId;
        initComponents();
        initControls();
        doCellColoring();
        m_iPanBehandlung = iPanBehandlung;
        
        new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(5000); 
                        m_tableModel.update();
                        doCellColoring();
                        jXTable1.repaint();
                    }
                }
                catch(InterruptedException ex) {}
            }
        }).start();
        if(jXTable1.getRowCount() > 0){
            jXTable1.setRowSelectionInterval(0, 0);
        }
    }

    private void doCellColoring() {

        TableColumnModel colModel = jXTable1.getColumnModel();
        WarteschlangeCellRender tableCellRenderer = new WarteschlangeCellRender(m_tableModel);
        colModel.getColumn(0).setMaxWidth(50);
        for(int i = 1; i < jXTable1.getColumnCount(); i++){
            colModel.getColumn(i).setCellRenderer(tableCellRenderer);
        }
        
    }
    
    private void initControls() {
        m_tableModel = new WarteschlangeArzt(m_controller, m_arztId);
        jXTable1.setModel(m_tableModel);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jPanel1 = new javax.swing.JPanel();

        jButton1.setText("Patient behandeln");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Patient entfernen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Patienten in Warteschlange");

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"01.", "Patrick Pimmel"},
                {"02.", "Peter Klo"},
                {"03.", "Josef Strumpf"},
                {"04.", "Hicke Hacke"},
                {"05.", "Axel Schweiss"},
                {"06.", "Dagobert Gack"},
                {"07", "Klaus Grauenhaft"},
                {"08.", "Anke Unke"}
            },
            new String [] {
                "Position", "Patient"
            }
        ));
        jXTable1.setSortable(false);
        jScrollPane1.setViewportView(jXTable1);
        jXTable1.getColumnModel().getColumn(0).setMinWidth(60);
        jXTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
        jXTable1.getColumnModel().getColumn(0).setMaxWidth(60);
        jXTable1.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 583, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 27, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addContainerGap())
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel1)
                .add(12, 12, 12)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Integer patientId = getSelectedPatient();
        if(patientId != null){
            m_controller.ausreihen(patientId);
            reInitializeTable(0);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    @SuppressWarnings("empty-statement")
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Integer patientId = getSelectedPatient();
        if(patientId != null){
            IOcumed dh = dh = OcumedFactory.getInstance();
            IPatient pat = dh.getiPatient(patientId);
            reInitializeTable(0);
            if(m_iPanBehandlung.createBehandlung(patientId) == true){
                m_controller.ausreihen(patientId);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void reInitializeTable(int direction){
         int row = jXTable1.getSelectedRow();
         int col = jXTable1.getSelectedColumn();
         Integer patientId = m_tableModel.getSelectedPatient(jXTable1.getSelectedRow() + direction, jXTable1.getSelectedColumn());
         m_tableModel.buildTable();
         doCellColoring();
         if(patientId != null && jXTable1.getRowCount() > row+direction){
            jXTable1.changeSelection(row+direction, col, false, false);
         } else {
            jXTable1.changeSelection(row, col, false, false);
         }
         jXTable1.repaint();
         paint(this.getGraphics());
    }
    
    private Integer getSelectedPatient(){
        return m_tableModel.getSelectedPatient(jXTable1.getSelectedRow(), jXTable1.getSelectedColumn());
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables


}
